import { createRouter, createWebHistory } from 'vue-router'
import Home from "@/views/home.vue"
import NotFound from "@/views/404.vue"
import Login from "@/views/login.vue"
import Admin from "@/layout/admin.vue"
import GoodsList from "@/views/goods/list.vue"
import CategoryList from "@/views/category/list.vue"
import UserList from "@/views/user/list.vue"
import CouponList from "@/views/coupon/list.vue"
import OrderList from "@/views/order/list.vue"
import CommentList from "@/views/comment/list.vue"
import ImageList from "@/views/image/list.vue"
import NoticeList from "@/views/notice/list.vue"
import SettingList from "@/views/setting/base.vue"
import Managerlist from '@/views/manager/list.vue'

 
// 默认路由
const routes= [
      {
        path:"/",
        name:'admin',
        component:Admin,
        
      },
      { path: '/:pathMatch(.*)*', 
        name: 'NotFound', 
        component: NotFound ,
        meta:{
          title:'404'
        }
      },
      {
        path:"/login",
        component:Login ,
        meta:{
          title:'登录页'
        }
      },
    
  ]


  // 动态路由
  const asyncRoutes=[
    {
      path:'/',
      name:'/',
      component:Home,
      meta:{
        title:'后台首页'
      }
    },
    {
      path:'/goods/list',
      name:'/goods/list',
      component:GoodsList,
      meta:{
        title:'商品管理'
      }
    },
    {
      path:'/category/list',
      name:'/category/list',
      component:CategoryList,
      meta:{
        title:'分类列表'
      }
    },
    {
      path:'/user/list',
      name:'/user/list',
      component:UserList,
      meta:{
        title:'用户列表'
      }
    }
    ,
    {
      path:'/order/list',
      name:'/order/list',
      component:OrderList,
      meta:{
        title:'订单列表'
      }
    }
    ,
    {
      path:'/comment/list',
      name:'/comment/list',
      component:CommentList,
      meta:{
        title:'评价列表'
      }
    }
    ,
    {
      path:'/image/list',
      name:'/image/list',
      component:ImageList,
      meta:{
        title:'图库列表'
      }
    }
    ,
    {
      path:'/notice/list',
      name:'/notice/list',
      component:NoticeList,
      meta:{
        title:'公告列表'
      }
    }
    ,
    {
      path:'/setting/base',
      name:'/setting/base',
      component:SettingList,
      meta:{
        title:'配置'
      }
    }
    ,
    {
      path:'/coupon/list',
      name:'/coupon/list',
      component:CouponList,
      meta:{
        title:'优惠券列表'
      }
    },
    {
      path:'/manager/list',
      name:'/manager/list',
      component:Managerlist,
      meta:{
        title:'管理员列表'
      }
    }
  ]
 export const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 动态添加路由的方法
// export function addRoutes (menus){
//   const findAndAddRoutesByMenus=(arr)=>{
//     arr.forEach(e => {
    
//      let item= asyncRoutes.find(o=>o.path==e.frontpath )
//       if(item && !router.hasRoute(item.path) ){
//        router.addRoute('admin',item) 
//       }
//       if(e.child && e.child.length >0){
//         findAndAddRoutesByMenus(e.child)
//       }
//     });
//   }
//   findAndAddRoutesByMenus(menus)
//   console.log(router.getRoutes());
// }
export function addRoutes(menus){
  // 是否有新的路由
  let hasNewRoutes = false
  const findAndAddRoutesByMenus = (arr) =>{
      arr.forEach(e=>{
          let item = asyncRoutes.find(o=>o.path == e.frontpath)
          if(item && !router.hasRoute(item.path)){
              router.addRoute("admin",item)
              hasNewRoutes = true
          }
          if(e.child && e.child.length > 0){
              findAndAddRoutesByMenus(e.child)
          }
      })
  }

  findAndAddRoutesByMenus(menus)

  return hasNewRoutes
}



